package com.ruoyi.web.controller.item;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.item.domain.ItemGoodssell;
import com.ruoyi.item.service.IItemGoodssellService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 在线商品销售Controller
 * 
 * @author ruoyi
 * @date 2022-03-23
 */
@RestController
@RequestMapping("/item/goodssell")
public class ItemGoodssellController extends BaseController
{
    @Autowired
    private IItemGoodssellService itemGoodssellService;

    /**
     * 查询在线商品销售列表
     */
    @PreAuthorize("@ss.hasPermi('item:goodssell:list')")
    @GetMapping("/list")
    public TableDataInfo list(ItemGoodssell itemGoodssell)
    {
        startPage();
        List<ItemGoodssell> list = itemGoodssellService.selectItemGoodssellList(itemGoodssell);
        return getDataTable(list);
    }

    /**
     * 导出在线商品销售列表
     */
    @PreAuthorize("@ss.hasPermi('item:goodssell:export')")
    @Log(title = "在线商品销售", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItemGoodssell itemGoodssell)
    {
        List<ItemGoodssell> list = itemGoodssellService.selectItemGoodssellList(itemGoodssell);
        ExcelUtil<ItemGoodssell> util = new ExcelUtil<ItemGoodssell>(ItemGoodssell.class);
        util.exportExcel(response, list, "在线商品销售数据");
    }

    /**
     * 获取在线商品销售详细信息
     */
    @PreAuthorize("@ss.hasPermi('item:goodssell:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return AjaxResult.success(itemGoodssellService.selectItemGoodssellByGoodsId(goodsId));
    }

    /**
     * 新增在线商品销售
     */
    @PreAuthorize("@ss.hasPermi('item:goodssell:add')")
    @Log(title = "在线商品销售", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItemGoodssell itemGoodssell)
    {
        return toAjax(itemGoodssellService.insertItemGoodssell(itemGoodssell));
    }

    /**
     * 修改在线商品销售
     */
    @PreAuthorize("@ss.hasPermi('item:goodssell:edit')")
    @Log(title = "在线商品销售", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItemGoodssell itemGoodssell)
    {
        return toAjax(itemGoodssellService.updateItemGoodssell(itemGoodssell));
    }

    /**
     * 删除在线商品销售
     */
    @PreAuthorize("@ss.hasPermi('item:goodssell:remove')")
    @Log(title = "在线商品销售", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(itemGoodssellService.deleteItemGoodssellByGoodsIds(goodsIds));
    }

    @Log(title = "商品管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ItemGoodssell> util = new ExcelUtil<ItemGoodssell>(ItemGoodssell.class);
        List<ItemGoodssell> goodssellList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = itemGoodssellService.importGoodssell(goodssellList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ItemGoodssell> util = new ExcelUtil<ItemGoodssell>(ItemGoodssell.class);
        util.importTemplateExcel(response, "商品数据");
    }
}
